/**
 * 
 */
package com.laozhao.dreamframe.resources;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import com.laozhao.dreamframe.util.ToolsUtil;

/**
 * @author dozhao
 * 
 */
public class DreamResource {

	private ResourceBundle rsb;
	private String resourcePath = "DreamTable";
	private Locale locale = Locale.getDefault();

	//
	// private int bufferSize = 200;
	private Map<String, String> bufferMap;

	public DreamResource() {
		rsb = ResourceBundle.getBundle(resourcePath, locale);
		bufferMap = new HashMap<String, String>();
	}

	public DreamResource(String resourcePath) {
		this.resourcePath = resourcePath;
		rsb = ResourceBundle.getBundle(resourcePath, locale);
		bufferMap = new HashMap<String, String>();
	}

	public DreamResource(String resourcePath, Locale locale) {
		this.resourcePath = resourcePath;
		this.locale = locale;
		rsb = ResourceBundle.getBundle(resourcePath, locale);
		bufferMap = new HashMap<String, String>();
	}

	public String getString(String key) {
		if (key != null && !"".equals(key)) {
			if (rsb.containsKey(key)) {
				return rsb.getString(key);
			} else {
				if (!bufferMap.containsKey(key) ) {
					bufferMap.put(key, "|" + key + "|");
					// if (bufferMap.size() > bufferSize) {
					bufferSave(key);
					// }
				}
				return "|" + key + "|";
			}
		}
		return "";
	}

	private void bufferSave(String key) {
		String bundleName = ToolsUtil.getRoot() + resourcePath + ".properties";
		File propertiesFile = new File(bundleName);
		if (!propertiesFile.exists()) {
			try {
				propertiesFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
				return;
			}
		}
		try {
			// OutputStream stream = new FileOutputStream(propertiesFile,true);
			// OutputStreamWriter writer = new
			// OutputStreamWriter(stream,"iso8859-1");
			Writer writer = new FileWriter(propertiesFile, true);
			BufferedWriter bw = new BufferedWriter(writer);

			StringBuffer properties = new StringBuffer();
			// for (Iterator<String> iterator = bufferMap.keySet().iterator();
			// iterator
			// .hasNext();) {
			// String key = iterator.next();
			properties.append(key).append("\t=\t").append(key);
			// .append("\n");
			// }
			bw.write(properties.toString());
			bw.newLine();
			// if (bufferMap.size() > bufferSize) {
			// writer.flush();
			// rsb = ResourceBundle.getBundle(resourcePath, locale);
			// bufferMap.clear();
			// }
			bw.close();
			writer.close();
			// stream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return;
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
	}

	public Locale getLocale() {
		return locale;
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}

	public String getResourcePath() {
		return resourcePath;
	}

	public void setResourcePath(String resourcePath) {
		this.resourcePath = resourcePath;
	}

}
